
import sys
from distutils.util import get_platform

plat_specifier = ".%s-%s" % (get_platform(), sys.version[0:3])
sys.path.append("./build/lib%s" % (plat_specifier))


def test_import():
    import jjregion

import jjregion

def test_simple():
    r = jjregion.Circle(10, 15, 3)
    ii = r.inside([11,11], [16,18])
    assert ii[0] == 1
    assert ii[1] == 0

def test_transform():
    r1 = jjregion.Circle(10, 15, 3)
    r2 = jjregion.Transformed(r1, 10, 15, 3, 3, 0, 0)
    ii = r2.inside([11,11], [16,18])
    assert ii[0] == 1
    assert ii[1] == 0

def test_2d():
    r1 = jjregion.Circle(10, 15, 3)
    r2 = jjregion.Transformed(r1, 10, 15, 3, 3, 0, 0)
    ii = r2.inside([[11,11], [11,11]], [[16,18], [16,18]])
    assert ii.shape == (2,2)

def test_mask():
    r1 = jjregion.Circle(25, 6, 10)
    ii = r1.mask((40, 40))
    assert ii.shape == (40,40)

def test_not():
    r1 = jjregion.Circle(10, 15, 3)
    #r2 = jjregion.RegionNot(r1)
    r2 = ~r1
    ii = r2.inside([11,11], [16,18])
    assert ii[0] == False
    assert ii[1] == True
    
def test_or():
    r1 = jjregion.Circle(10, 15, 3)
    r2 = jjregion.Circle(15, 25, 3)
    r12 = r1 | r2
    #r12 = jjregion.RegionOr(r1, r2)
    ii = r12.inside([11,11,15], [16,18,25])
    assert ii[0] == 1
    assert ii[1] == 0
    assert ii[2] == 1


def test_regionList_checktype():
    r1 = jjregion.Circle(10, 15, 3)
    r2 = jjregion.Circle(15, 25, 3)

    r12 = jjregion.RegionList(r1, r2)

    try:
        r12 = jjregion.RegionList(r1, r2, 3)
    except TypeError:
        pass
    else:
        raise "Failed to catch exception"
    


def test_orList():
    r1 = jjregion.Circle(10, 15, 3)
    r2 = jjregion.Circle(15, 25, 3)
    r3 = jjregion.Circle(15, 25, 3)

    r123 = jjregion.RegionOrList(r1, r2, r3)
    ii = r123.inside([11,11,15], [16,18,25])
    assert ii[0] == 1
    assert ii[1] == 0
    assert ii[2] == 1


def test_mask_rotated():
    r1 = jjregion.Box(25, 50, 10,5)
    r2 = jjregion.Rotated(r1, 30./180.*3.141592, 25, 50)
    i1 = r1.mask((120, 100))
    i2 = r2.mask((120, 100))
    assert i2.shape == (120,100)

def test_mask_polygon():
    r1 = jjregion.Polygon([49, 69, 26], [80, 42, 42])
    i1 = r1.mask((120, 100))
    assert i1.shape == ((120,100))

def test_mask_translated():
    r1 = jjregion.Box(25, 50, 10,5)
    r2 = jjregion.Translated(r1, 30, -20)
    i1 = r1.mask((110, 100))
    i2 = r2.mask((110, 100))
    assert i2.shape == (110,100)



def aaa():
    ii = r.inside(11, 16)
    assert (ii==1)
    ii = r.inside(11, 18)
    assert (ii==0)
    
    r123 = Or(r1, r2, r3)
    r123[2] = r3p

    r123[1:].inside(x, y)
    




test_string_1 = r""" # cooo
circle(109,253,28.950304) # comment
polygon(257,290,300.78944,271.78944,300.78944,178.21056,258,216,207.21056,178.21056)
polygon(273.98971,175.01029,274.01029,175.01029,274.01029,174.98971,273.98971,174.98971)
-rotbox(162,96.5,134,41,43.801537)
and ellipse(172,328,23,41,27.300813)"""
test_string_1_shape_name_list = ["circle", "polygon", "polygon", "rotbox", "ellipse"]

import jjregion as reg
import jjregion.parser  as RP

class TestFactory(object):
    def __init__(self, ):
        """
        """
        self.rf = reg.RegionFactory()
        self.rp = RP.RegionParser(self.rf)

    def test_circle(self, ):
        """
        """
        input_string = "circle(109,253,28.950304)"
        r = self.rp.parseString(input_string)
        print r
        assert isinstance(r, reg.Circle)

    def test_circle_box(self, ):
        """
        """
        input_string = "circle(109,253,28.950304) and rotbox(109,253,28.950304, 304., 10)"
        r = self.rp.parseString(input_string)
        print r
        assert isinstance(r[0], reg.Circle)
        #print r[1].child
        assert isinstance(r[1].child, reg.Box)
        assert isinstance(r, reg.RegionAndList)
        #raise

    
    def test_circle_box_ellipse(self, ):
        """
        """
        input_string = ["circle(109,253,28.950304)",
                        "rotbox(19,53,8.950304, 3., 10.)",
                        "ellipse(9,22,2.9, 3., 10.)"]
        r = self.rp.parseString(" ".join(input_string))
        print r
        assert isinstance(r[0], reg.Circle)
        print 
        assert isinstance(r[1].child, reg.Box)
        assert isinstance(r[2].child, reg.Ellipse)
        assert isinstance(r, reg.RegionOrList)
        #raise

    def test_polygon(self, ):
        """
        """
        input_string = "polygon(257,290,300.78944,271.78944,300.78944,178.21056,258,216,207.21056,178.21056)"

        r = self.rp.parseString(input_string)
        print r
        assert isinstance(r, reg.Polygon)
        #raise

    def test_comment(self, ):
        test_string_1 = r"""# comment 1
circle(109,253,28.950304) # comment 2
polygon(257,290,300.78944,271.78944,300.78944,178.21056,258,216,207.21056,178.21056)
polygon(273.98971,175.01029,274.01029,175.01029,274.01029,174.98971,273.98971,174.98971)
-rotbox(162,96.5,134,41,43.801537)
and ellipse(172,328,23,41,27.300813)"""
        input_string = test_string_1

        r = self.rp.parseString(input_string)
        
        rc = self.rp.comment_list
        assert rc[0][0] is None
        assert rc[0][1].strip() == "comment 1"
        assert isinstance(rc[1][0], reg.Circle)
        assert rc[1][1].strip() == "comment 2"
        #raise

    def test_global(self, ):
        input_string = "global a=3 f=3\npolygon(257,290,300.78944,271.78944,300.78944,178.21056,258,216,207.21056,178.21056)"

        r = self.rp.parseString(input_string)
        rg =  self.rp.global_list
        print rg
        assert rg[0].strip() == "a=3 f=3"
        #raise
